import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, Arc
import matplotlib.lines as lines
from matplotlib.transforms import Affine2D
import matplotlib.patches as patches

# 设置中文显示
plt.rcParams["font.family"] = ["SimHei", "WenQuanYi Micro Hei", "Heiti TC"]
plt.rcParams["axes.unicode_minus"] = False  # 正确显示负号

# 创建一个包含三个子图的图形
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(18, 6))
fig.suptitle("无人机、导弹及烟幕弹相关示意图", fontsize=16)

# ------------------- 图2：无人机、导弹及烟幕弹的运动阶段与轨迹 -------------------
ax1.set_title("图2：运动阶段与轨迹示意图")
ax1.set_xlabel("水平距离 (m)")
ax1.set_ylabel("高度 (m)")
ax1.set_xlim(0, 1000)
ax1.set_ylim(0, 600)
ax1.grid(True, linestyle='--', alpha=0.7)

# 无人机轨迹
drone_x = np.linspace(0, 1000, 200)
drone_y = 500 + 50 * np.sin(drone_x / 100)
ax1.plot(drone_x, drone_y, 'b-', linewidth=2, label='无人机轨迹')

# 导弹轨迹
missile_x = np.linspace(200, 900, 150)
missile_y = 400 - (missile_x - 200) ** 2 / 1000
ax1.plot(missile_x, missile_y, 'r-', linewidth=2, label='导弹轨迹')

# 烟幕弹轨迹
smoke_x = np.linspace(300, 700, 100)
smoke_y = 350 - (smoke_x - 300) ** 2 / 600

# 分段绘制烟幕弹轨迹，区分不同阶段
launch_idx = 20  # 投放阶段结束点
detonate_idx = 60  # 起爆点
ax1.plot(smoke_x[:launch_idx], smoke_y[:launch_idx], 'g-', linewidth=2)
ax1.plot(smoke_x[launch_idx:detonate_idx], smoke_y[launch_idx:detonate_idx], 'g--', linewidth=2)
ax1.plot(smoke_x[detonate_idx:], smoke_y[detonate_idx:], 'g-.', linewidth=2)

# 标注关键阶段
ax1.scatter(smoke_x[0], smoke_y[0], c='g', s=80, zorder=5)
ax1.text(smoke_x[0]-50, smoke_y[0]+30, '投放点', color='g', fontsize=10)

ax1.scatter(smoke_x[launch_idx], smoke_y[launch_idx], c='g', s=80, zorder=5)
ax1.text(smoke_x[launch_idx]+10, smoke_y[launch_idx]-50, '投放阶段结束', color='g', fontsize=10)

ax1.scatter(smoke_x[detonate_idx], smoke_y[detonate_idx], c='orange', s=100, zorder=5)
ax1.text(smoke_x[detonate_idx]+10, smoke_y[detonate_idx]+30, '起爆点', color='orange', fontsize=10)

# 绘制烟幕扩散
smoke_circle = Circle((smoke_x[detonate_idx], smoke_y[detonate_idx]), 80, 
                     fill=True, color='gray', alpha=0.3, zorder=3)
ax1.add_patch(smoke_circle)

# 标注下沉阶段
ax1.text(600, 150, '下沉阶段', color='g', fontsize=10)

ax1.legend()

# ------------------- 图3：烟幕云团遮蔽导弹-真目标视线的几何判定 -------------------
ax2.set_title("图3：烟幕遮蔽几何判定示意图")
ax2.set_xlabel("X坐标 (m)")
ax2.set_ylabel("Y坐标 (m)")
ax2.set_xlim(0, 800)
ax2.set_ylim(0, 600)
ax2.grid(True, linestyle='--', alpha=0.7)

# M1位置
m1_x, m1_y = 200, 400
ax2.scatter(m1_x, m1_y, c='blue', s=100, zorder=5)
ax2.text(m1_x-30, m1_y+30, 'M1位置', color='blue', fontsize=10)

# 真目标采样点
target_x, target_y = 600, 300
ax2.scatter(target_x, target_y, c='red', s=100, zorder=5)
ax2.text(target_x+10, target_y+10, '真目标采样点', color='red', fontsize=10)

# 绘制视线
line_of_sight = lines.Line2D([m1_x, target_x], [m1_y, target_y], 
                            color='black', linestyle='-', linewidth=1.5, zorder=2)
ax2.add_line(line_of_sight)

# 烟幕球
smoke_center_x, smoke_center_y = 400, 350
smoke_radius = 100
smoke_circle = Circle((smoke_center_x, smoke_center_y), smoke_radius, 
                     fill=True, color='gray', alpha=0.3, zorder=3)
ax2.add_patch(smoke_circle)
ax2.text(smoke_center_x-50, smoke_center_y-120, '烟幕球', color='gray', fontsize=10)

# 计算视线与烟幕球的交点
dx = target_x - m1_x
dy = target_y - m1_y
a = dx**2 + dy**2
b = 2 * (dx * (m1_x - smoke_center_x) + dy * (m1_y - smoke_center_y))
c = (m1_x - smoke_center_x)**2 + (m1_y - smoke_center_y)** 2 - smoke_radius**2
discriminant = b**2 - 4*a*c

if discriminant > 0:
    t1 = (-b + np.sqrt(discriminant)) / (2*a)
    t2 = (-b - np.sqrt(discriminant)) / (2*a)
    
    # 交点
    intersect1_x = m1_x + t1 * dx
    intersect1_y = m1_y + t1 * dy
    intersect2_x = m1_x + t2 * dx
    intersect2_y = m1_y + t2 * dy
    
    ax2.scatter([intersect1_x, intersect2_x], [intersect1_y, intersect2_y], 
               c='purple', s=50, zorder=5)
    ax2.text(intersect1_x+10, intersect1_y+10, '交点', color='purple', fontsize=9)

# ------------------- 图4：无人机速度与烟幕遮蔽窗口的约束冲突 -------------------
ax3.set_title("图4：速度与烟幕遮蔽窗口约束冲突")
ax3.set_xlabel("时间 (s)")
ax3.set_ylabel("无人机速度/烟幕效果")
ax3.set_xlim(0, 20)
ax3.set_ylim(0, 1.2)
ax3.grid(True, linestyle='--', alpha=0.7)

# 时间轴
time = np.linspace(0, 20, 200)

# 烟幕有效时段
smoke_effective = np.zeros_like(time)
smoke_effective[(time >= 5) & (time <= 15)] = 1.0
ax3.fill_between(time, 0, smoke_effective, color='gray', alpha=0.3, label='烟幕有效时段')

# 不同速度下M1的飞行时段
# 低速情况
low_speed_start, low_speed_end = 3, 10
ax3.fill_between([low_speed_start, low_speed_end], [0.2, 0.2], [0.4, 0.4], 
                color='blue', alpha=0.6, label='低速M1飞行时段')

# 中速情况
mid_speed_start, mid_speed_end = 7, 13
ax3.fill_between([mid_speed_start, mid_speed_end], [0.5, 0.5], [0.7, 0.7], 
                color='green', alpha=0.6, label='中速M1飞行时段')

# 高速情况
high_speed_start, high_speed_end = 10, 16
ax3.fill_between([high_speed_start, high_speed_end], [0.8, 0.8], [1.0, 1.0], 
                color='red', alpha=0.6, label='高速M1飞行时段')

# 标注重叠区域
ax3.text(6, 0.25, '重叠区', color='black', fontsize=9)
ax3.text(9, 0.55, '重叠区', color='black', fontsize=9)
ax3.text(12, 0.85, '重叠区', color='black', fontsize=9)

ax3.legend(loc='upper right')

# 调整布局
plt.tight_layout()

# 显示图形
plt.show()
